package com.example.demo.study;

public class Test2 {
    public static void main(String[] args) {
        String str = "12abc-abCABc-4aB@";
        Integer k = 3;
        String convert = convert(str, k);
        System.out.println(convert);
    }

    public static String convert(String str, Integer k) {
        int index = str.indexOf("-");
        int length = str.length() - index - 1;

        StringBuilder sb = new StringBuilder();
        sb.append(str, 0, index + 1);

        StringBuilder sub = new StringBuilder();
        int upNum = 0;
        int lowerNum = 0;
        for (int i = 0; i < length; i++) {
            char c = str.charAt(index + 1 + i);
            if (c != '-') {
                sub.append(c);
                if (c >= 'A' && c <= 'Z') {
                    upNum++;
                }
                if (c >= 'a' && c <= 'z') {
                    lowerNum++;
                }
                if (sub.length() == k) {
                    if (upNum > lowerNum) {
                        sb.append(sub.toString().toUpperCase());
                    } else if (upNum < lowerNum) {
                        sb.append(sub.toString().toLowerCase());
                    } else {
                        sb.append(sub.toString());
                    }
                    sb.append("-");
                    sub = new StringBuilder();
                    upNum = 0;
                    lowerNum = 0;
                }
            }
        }
        if (sub.length() > 0) {
            if (upNum > lowerNum) {
                sb.append(sub.toString().toUpperCase());
            } else if (upNum < lowerNum) {
                sb.append(sub.toString().toLowerCase());
            } else {
                sb.append(sub.toString());
            }
        }
        String res = sb.toString();
        if (res.endsWith("-")) {
            return res.substring(0, res.length() - 1);
        }
        return res;
    }


}
